package com.cslc.wechat.bind.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cslc.wechat.bind.service.BindService;
import com.cslc.wechat.common.bean.Clerk;
import com.cslc.wechat.common.bean.Terminal;
import com.cslc.wechat.common.dao.HibernateDao;
import com.cslc.wechat.common.service.WechatService;

public class BindServiceImpl implements BindService{
	
	private static Logger logger = Logger.getLogger(BindServiceImpl.class);
	
	private HibernateDao hibernateDao;
	private WechatService wechatService;
	public HibernateDao getHibernateDao() {
		return hibernateDao;
	}
	public void setHibernateDao(HibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	public void saveOrUpdateClerk(Clerk clerk) {
		boolean flag = false;    	
    	String hql = "from Clerk c where c.openid = ?";
    	Object[] value = {clerk.getOpenid()};
    	List list = hibernateDao.findByParams(hql, value);
    	
    	if(null != list && 0 < list.size()) {
    		//获取持久态对象clerk
    		Clerk persClerk = (Clerk)list.get(0);
    		//更新持久态clerk
    		persClerk.setSubscribe_time(clerk.getSubscribe_time());
    		persClerk.setNickname(clerk.getNickname());
    		persClerk.setSubscribe(clerk.getSubscribe());
    		persClerk.setSex(clerk.getSex());
    		persClerk.setCity(clerk.getCity());
    		persClerk.setCountry(clerk.getCountry());
    		persClerk.setGroup_id(clerk.getGroup_id());
    		persClerk.setLanguage(clerk.getLanguage());
    		persClerk.setProvince(clerk.getProvince());
    		persClerk.setRemark(clerk.getRemark());
    		persClerk.setHeadimgurl(clerk.getHeadimgurl());

    		hibernateDao.update(persClerk);
    	} else {
    		hibernateDao.insert(clerk);
    	}
	}
	@Override
	public Clerk getClerkFromWeChatService(String openid) {
		Clerk clerk = new Clerk();
    	clerk.setOpenid(openid);
    	
    	//调用微信接口获取用户基本信息，json格式
		JSONObject jsonObject = wechatService.getUserInfo(openid);
    	logger.info("查询用户信息："+jsonObject);
    	
    	//将json转换成对象
    	if(null != jsonObject) {
	    		clerk.setSubscribe(jsonObject.get("subscribe").toString());	    	
		    	clerk.setNickname(jsonObject.get("nickname").toString());
		    	clerk.setSex(jsonObject.get("sex").toString());
		    	clerk.setLanguage(jsonObject.get("language").toString());
		    	clerk.setCity(jsonObject.get("city").toString());
		    	clerk.setProvince(jsonObject.get("province").toString());
		    	clerk.setCountry(jsonObject.get("country").toString());
		    	clerk.setHeadimgurl(jsonObject.get("headimgurl").toString());
		    	clerk.setSubscribe_time(jsonObject.get("subscribe_time").toString());
		    	clerk.setRemark(jsonObject.get("remark").toString());
		    	clerk.setGroup_id(jsonObject.get("groupid").toString());
	    }
    	logger.info("查询用户信息："+clerk.toString());
    	return clerk;
	}
	@Override
	public String bindClerkAndTerminal(String openid, String terminalNo, String tel) {
    	String resultMessage = null;
		String clerkHql = "from Clerk c where c.openid = ?";
    	String terminalHql = "from Terminal t where t.terminalNo = ?";
		Clerk persClerk = (Clerk) hibernateDao.findUniqueObjByParam(clerkHql, openid);
    	Terminal persTerminal = (Terminal)hibernateDao.findUniqueObjByParam(terminalHql, terminalNo);
    	
    	if(null != persClerk && null != persTerminal) {
    		Set<Terminal> terminals = persClerk.getTerminals();
    		if(terminals.contains(persTerminal)) {
    			resultMessage = "duplicate";
    			logger.info("请勿重复绑定：" + openid + "  " + terminalNo);
    		} else {
    			persClerk.addToTerminal(persTerminal);	
    			persClerk.setTel(tel);
    			hibernateDao.update(persClerk);
    			resultMessage = "success";
    		}
    	}
    	else if(null != persTerminal){
    		logger.error("系统中不存在该用户: " + openid);
    		resultMessage = "系统中不存在该用户"; 
    	}
    	else if(null != persClerk) {
    		logger.error("系统中不存在该终端: " + terminalNo);
    		resultMessage = "系统中不存在该终端";
    	}
    	else {
    		logger.error("即不存在该用户，也不存在该终端");
    		resultMessage = "系统中不存在该用户，也不存在该终端";
    	}
    	return resultMessage;
	}
	
	@Override
	public void unBindClerkAndTerminal(String openid) {
		String clerkHql = "from Clerk c where c.openid = ?";
		Clerk persClerk = (Clerk) hibernateDao.findUniqueObjByParam(clerkHql, openid);
    	
    	if(null != persClerk) {
    		Set<Terminal> terminals = persClerk.getTerminals();
    		Iterator it = terminals.iterator();
    		while(it.hasNext()) {
    			Terminal terminal = (Terminal) it.next();
    			persClerk.removeFromTerminal(terminal);
    			hibernateDao.update(persClerk);
    			return;
    		}
    	}
	}
	
	@Override
	public Set<Terminal> getClerkBindTerminals(String openid) {
		Set<Terminal> terminals = null;
		//获取clerk对象
		String hql = "from Clerk c where c.openid = ?";
		String param = openid;
		Clerk clerk = (Clerk) hibernateDao.findUniqueObjByParam(hql, param);
		terminals = clerk.getTerminals();
		return terminals;
	}
	
	
	
	public WechatService getWechatService() {
		return wechatService;
	}
	public void setWechatService(WechatService wechatService) {
		this.wechatService = wechatService;
	}
	
	
}
